home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_3698.txt < prev    next >
Text File  |  1990-04-17  |  11KB  |  377 lines

  1. -- card: 3698 from stack: in.3
  2. -- bmap block id: 0
  3. -- flags: 4000
  4. -- background id: 3241
  5. -- name: PICTFileToRes
  6. ----- HyperTalk script -----
  7. on Install
  8.   get ChooseTargetStack()
  9.   InstallResource XFCN,PICTFileToRes,it
  10. end Install
  11.  
  12.  
  13. -- part 1 (button)
  14. -- low flags: 00
  15. -- high flags: A003
  16. -- rect: left=83 top=300 right=322 bottom=183
  17. -- title width / last selected line: 0
  18. -- icon id / first selected line: 0 / 0
  19. -- text alignment: 1
  20. -- font id: 0
  21. -- text size: 12
  22. -- style flags: 0
  23. -- line height: 16
  24. -- part name: PICTFileToRes
  25. ----- HyperTalk script -----
  26. on mouseUp
  27.   put PICTFileToRes()
  28. end mouseUp
  29.  
  30.  
  31.  
  32. -- part 3 (button)
  33. -- low flags: 00
  34. -- high flags: A003
  35. -- rect: left=299 top=300 right=322 bottom=438
  36. -- title width / last selected line: 0
  37. -- icon id / first selected line: 0 / 0
  38. -- text alignment: 1
  39. -- font id: 0
  40. -- text size: 12
  41. -- style flags: 0
  42. -- line height: 16
  43. -- part name: Show Pascal Source
  44. ----- HyperTalk script -----
  45. on mouseUp
  46.   set the visible of card field 1 to not the visible of card field 1
  47.   if the visible of card field 1 is true then
  48.     set the name of me to "Hide Pascal Source"
  49.   else set the name of me to "Show Pascal Source"
  50. end mouseUp
  51.  
  52.  
  53.  
  54. -- part 4 (field)
  55. -- low flags: 81
  56. -- high flags: 2007
  57. -- rect: left=12 top=26 right=298 bottom=491
  58. -- title width / last selected line: 0
  59. -- icon id / first selected line: 0 / 0
  60. -- text alignment: 0
  61. -- font id: 22
  62. -- text size: 10
  63. -- style flags: 0
  64. -- line height: 13
  65. -- part name: Source
  66.  
  67.  
  68. -- part contents for background part 16
  69. ----- text -----
  70. PICTFILETORES XFCN version 1.6
  71. Kevin Calhoun
  72.  
  73. PICTFileToRes creates a PICT resource from a PICT file and copies it into the current stack.  
  74. You can tell PICTFileToRes what ID number you want the PICT resource to have or you can let it select an unused number for you.  If you choose a number that belongs to another PICT resource currently contained in your stack, the new picture will overwrite the old one.
  75.  
  76. PICTFileToRes allows the user to choose the PICT file to copy from from a standard file dialog box.  If the user presses Cancel instead of choosing a file, PICTFileToRes returns 
  77. "Cancel".
  78.  
  79. As with other resource copiers, if you use PICTFileToRes to copy a PICT into the Home stack, you may have to quit and relaunch HyperCard in order to use it.
  80.  
  81. INVOKING PICTFILETORES
  82.  
  83. get PICTFileToRes(<pictID>,<"pictName">)
  84.  
  85. result:  resourceID
  86.  
  87. Both parameters are optional.  If you don't pass a value for pictID, PICTFileToRes will find an ID for the PICT resource that's not currently in use.  If you don't pass a value for pictName, the PICT resource will be unnamed.  If you pass a value for pictID or pictName that's already in use by another PICT resource in the current stack, the old PICT will be overwritten.
  88.  
  89. If an error occurs, PICTFileToRes will return an error message.  Word 1 of this message will be "Error."
  90.  
  91. EXAMPLES
  92.  
  93. put PICTFileToRes(0,"The Little Engine That Could") into pictNumber
  94. get PICTFileToRes
  95.  
  96. REVISION HISTORY
  97. 1.0 -- 4/22/88
  98. 1.5 -- 3/15/89 -- Altered source for compatibility with MPW Pascal 3.0.
  99. 1.6 -- 7/22/89  No longer leaves a NIL master pointer behind when replacing a resource.
  100.  
  101. -- part contents for card part 4
  102. ----- text -----
  103. UNIT PICTFileToResUnit;
  104.  
  105. { PICTFileToRes XFCN ┬⌐ 1988-1989 by the Trustees of Dartmouth College }
  106. { Written by Kevin Calhoun }
  107.  
  108. { This source compatible with MPW Pascal 3.0 }
  109.  
  110. (*
  111. Pascal PICTFileToRes.p
  112. Link -m ENTRYPOINT Γêé
  113.      -o "{boot}Hyper ╞Æ:Dartmouth XCMD's 3.1" Γêé
  114.      -rt XFCN=6483 Γêé
  115.      -sn Main=PICTFileToRes Γêé
  116.      PICTFileToRes.p.o Γêé
  117.      "{Libraries}"interface.o Γêé
  118.      "{PLibraries}"Paslib.o Γêé
  119.      "{Libraries}"HyperXLib.o
  120. *)
  121.  
  122. {$R-}
  123.  
  124. INTERFACE
  125.   USES
  126.     Types,
  127.     Memory,
  128.     Resources,
  129.     Files,
  130.     Errors,
  131.     Packages,
  132.     HyperXCmd;
  133.  
  134.   CONST
  135.     PictHeader = 512;
  136.  
  137.   PROCEDURE EntryPoint (paramPtr : XCMDPtr);
  138.  
  139. IMPLEMENTATION
  140.  
  141.   PROCEDURE ConvertPicture(paramPtr : XCMDPtr); FORWARD;
  142.  
  143.   PROCEDURE EntryPoint (paramPtr : XCMDPtr);
  144.   BEGIN
  145.     ConvertPicture(paramPtr);
  146.   END;
  147.  
  148.   FUNCTION GetScreenBitsBounds: Rect;
  149.   { get screenbits.bounds from the QuickDraw globals }
  150.   TYPE
  151.     LongwordPtr = ^LONGINT;
  152.     BitMapPtr = ^BitMap;
  153.   CONST
  154.     screenBitsOffset = -122;
  155.     CurrentA5 = $904;
  156.   VAR
  157.     screenBitsPtr : BitMapPtr;
  158.     myLongwordPtr : LongwordPtr;
  159.   BEGIN
  160.     myLongwordPtr := LongwordPtr(CurrentA5);
  161.       { myLongwordPtr now points to the pointer to the first QD global }
  162.     myLongwordPtr := LongwordPtr(myLongwordPtr^);
  163.       { myLongwordPtr now points to the first QD global }
  164.     screenBitsPtr := BitMapPtr(myLongwordPtr^ + screenBitsOffset);
  165.       { screenBitsPtr now points to the screenBits BitMap }
  166.     GetScreenBitsBounds := screenBitsPtr^.bounds;
  167.   END;
  168.  
  169.   FUNCTION GetTheNameOfThisStack (paramPtr : XCMDPtr; var str: Str255): OSErr;
  170.     VAR
  171.       theResult : Handle;
  172.       theLength : Longint;
  173.       err: OSErr;
  174.   BEGIN
  175.     err := noErr;
  176.     str := 'word 2 of the long name of this stack';
  177.     theResult := EvalExpr(paramPtr, str);
  178.     err := paramPtr^.result;
  179.     IF (theResult <> NIL) and (err = noErr) THEN
  180.       BEGIN
  181.       theLength := StringLength(paramPtr, theResult^);
  182.       ZeroToPas(paramPtr, theResult^, str);
  183.       DisposHandle(theResult);
  184.       DELETE(str,theLength,1);
  185.       DELETE(str,1,1);
  186.       END
  187.     ELSE str := '';
  188.     GetTheNameOfThisStack := err;
  189.   END;
  190.  
  191.   PROCEDURE ConvertPicture (paramPtr : XCMDPtr);
  192.     LABEL
  193.       98, 99, 100;
  194.     VAR
  195.       str : Str255;
  196.       myStack : INTEGER;
  197.       resAlready : Handle;
  198.       parameterCount : INTEGER;
  199.       id : INTEGER;
  200.       name : Str255;
  201.       SFGetReply : SFReply;
  202.       where : point;
  203.       theReadRefNum, curFile : INTEGER;
  204.       { file ref numbers for file manager calls }
  205.       err, closeErr : OSErr;
  206.       logEOF : longint;
  207.       theBufHndl : Handle;
  208.       gotName, gotID: BOOLEAN;
  209.  
  210.     PROCEDURE PassReturnValue (theMsg : Str255); { set theResult and quit }
  211.     BEGIN
  212.       paramPtr^.returnValue := PasToZero(paramPtr, theMsg);
  213.     END;
  214.  
  215.     PROCEDURE GetParameters;
  216.     BEGIN
  217.       gotID := FALSE;
  218.       gotName := FALSE;
  219.       name := '';
  220.       IF parameterCount > 0 THEN
  221.         BEGIN
  222.           ZeroToPas(paramPtr, paramPtr^.params[1]^, str);
  223.           if LENGTH(str) > 0 THEN gotID := TRUE;
  224.           id := StrToNum(paramPtr, str);
  225.           IF parameterCount > 1 THEN
  226.             BEGIN
  227.             ZeroToPas(paramPtr, paramPtr^.params[2]^, name);
  228.             if LENGTH(name) > 0 THEN gotName := TRUE;
  229.             END;
  230.         END;
  231.     END;
  232.  
  233.     PROCEDURE CheckForSameTypeIDName;
  234.     BEGIN
  235.       SetResLoad(FALSE);
  236.       IF not gotID THEN
  237.         REPEAT
  238.           id := Unique1ID('PICT');
  239.         UNTIL id > 127
  240.       ELSE
  241.         REPEAT
  242.           resAlready := Get1Resource('PICT', id);
  243.           IF resAlready <> NIL THEN
  244.             BEGIN
  245.               RmveResource(resAlready);
  246.               DisposHandle(resAlready);
  247.             END;
  248.         UNTIL resAlready = NIL;
  249.  
  250.       IF gotName THEN
  251.         REPEAT
  252.           resAlready := Get1NamedResource('PICT', name);
  253.           IF resAlready <> NIL THEN
  254.             BEGIN
  255.               RmveResource(resAlready);
  256.               DisposHandle(resAlready);
  257.             END;
  258.         UNTIL resAlready = NIL;
  259.       SetResLoad(TRUE);
  260.     END;
  261.  
  262.     PROCEDURE DoSFGet;
  263.       VAR
  264.         where : point;
  265.         typeList : SFTypeList;
  266.         dlgt: DialogTHndl;
  267.         r: rect;
  268.         screen: rect;
  269.         h, v: INTEGER;
  270.     BEGIN  { select text file to read using SFGetFile }
  271.       dlgt := DialogTHndl(GetResource('DLOG',getDlgID));
  272.       if dlgt <> nil then
  273.         begin
  274.         r := dlgt^^.boundsRect;
  275.         screen := GetScreenBitsBounds;
  276.         h := ((screen.right - screen.left) - (r.right - r.left)) div 2;
  277.         v := ((screen.bottom - screen.top) - (r.bottom - r.top)) div 2;
  278.         SetPt(where, h, v);
  279.         end
  280.       else SetPt(where, 82, 75);
  281.       typeList[0] := 'PICT';                { tell SFGetFile to display only text files }
  282.       SFGetFile(where, '', NIL, 1, typeList, NIL, SFGetReply);          { call SFGetFile }
  283.     END;
  284.  
  285.   BEGIN
  286.     err := noErr;
  287.     parameterCount := paramPtr^.paramCount;
  288.     IF parameterCount > 2 THEN
  289.       PassReturnValue('PICTFileToRes XFCN 1.6, 22 July 1989, ┬⌐1988-1989 Dartmouth College')
  290.     ELSE
  291.       BEGIN
  292.       GetParameters;
  293.       err := GetTheNameOfThisStack(paramPtr,str);
  294.       IF err<>noErr THEN GOTO 100;
  295.   
  296.       myStack := OpenResFile(str);
  297.       IF (myStack = -1) AND (ResError = eofErr) THEN
  298.         BEGIN
  299.           CreateResFile(str);
  300.           err := ResError;
  301.           IF err = noErr THEN
  302.             myStack := OpenResFile(str);
  303.         END;
  304.       IF (myStack <= 0) OR (err <> noErr) THEN GOTO 100;
  305.   
  306.       DoSFGet;
  307.       IF SFGetReply.good = FALSE THEN
  308.         BEGIN
  309.         PassReturnValue('Cancel');
  310.         GOTO 100;
  311.         END;
  312.       { continue only if user actually selected a file }
  313.   
  314.       WITH SFGetReply DO
  315.         err := FSOpen(fName, vRefNum, theReadRefNum);
  316.       { open the file }
  317.       IF err <> noErr THEN GOTO 100;
  318.       { continue only if file could be opened }
  319.   
  320.       err := GetEOF(theReadRefNum, logEOF);
  321.       IF err <> noErr THEN GOTO 99;
  322.       
  323.       { set up the buffer in memory for reading in logEOF bytes }
  324.       theBufHndl := NewHandle(logEOF - PictHeader);
  325.       err := MemError;
  326.       { save the result in case we want to report an error }
  327.       IF (theBufHndl = NIL) OR (err <> noErr) THEN GOTO 99;
  328.      { continue only if enough memory is available }
  329.   
  330.       MoveHHi(theBufHndl);
  331.       HLock(theBufHndl);  { lock down our buffer }
  332.   { read logEOF bytes into the location pointed to by theBufHndl^ }
  333.       err := SetFPos(theReadRefNum, fsFromStart, PictHeader);
  334.       IF err <> noErr THEN
  335.         BEGIN
  336.         DisposHandle(theBufHndl);
  337.         GOTO 99;
  338.         END;
  339.   
  340.       logEOF := logEOF - PictHeader;
  341.       err := FSRead(theReadRefNum, logEOF, theBufHndl^);
  342.       IF err <> noErr THEN
  343.         BEGIN
  344.         DisposHandle(theBufHndl);
  345.         GOTO 99;
  346.         END;
  347.       { continue only if the read worked }
  348.         
  349.       HNoPurge(theBufHndl);
  350.       curFile := CurResFile;
  351.       UseResFile(myStack);
  352.       CheckForSameTypeIDName;
  353.       AddResource(theBufHndl, 'PICT', id, name);
  354.       err := ResError;
  355.       IF err <> noErr THEN
  356.         BEGIN
  357.         DisposHandle(theBufHndl);
  358.         GOTO 98;
  359.         END;
  360.  
  361.       SetResAttrs(theBufHndl, resPurgeable + resChanged);
  362.       WriteResource(theBufHndl);
  363.       UpdateResFile(myStack);
  364.       NumToStr(paramPtr, id, str);
  365.       PassReturnValue(str);
  366.       ReleaseResource(theBufHndl);
  367.       98: UseResFile(curFile);
  368.       99: closeErr := FSClose(theReadRefNum);
  369.       100: if err <> noErr then
  370.         BEGIN
  371.         NumToStr(paramPtr, err, str);
  372.         PassReturnValue(CONCAT('Error ', str));
  373.         END;
  374.       END;
  375.   END;
  376.  
  377. END.